package dao;

import hibernate.HibernateUtil;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import dto.EmpleadoDTO;
import entities.DestinoEntity;
import entities.EmpleadoEntity;

public class EmpleadoDAO {
	private static EmpleadoDAO instancia = null;
	private static SessionFactory sf = null;
	
	private EmpleadoDAO(){}
	
	public static EmpleadoDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new EmpleadoDAO();
		} 
		return instancia;
	}
	
	public void grabarEmpleado(EmpleadoDTO empleado){
		EmpleadoEntity emp = new EmpleadoEntity();
		emp.setApellido(empleado.getApellido());
		emp.setDNI(empleado.getDNI());
		emp.setNombre(empleado.getNombre());
		Session session = sf.openSession();
		session.beginTransaction();
		session.persist(emp);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	
	public void actualizarEmpleado(EmpleadoDTO empleado){
		EmpleadoEntity emp = new EmpleadoEntity();
		emp.setApellido(empleado.getApellido());
		emp.setDNI(empleado.getDNI());
		emp.setNombre(empleado.getNombre());
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(emp);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	
	public EmpleadoEntity LoadEmpleado(int id) 
	{
		Session session = sf.openSession();
		session.beginTransaction();
		EmpleadoEntity ent = (EmpleadoEntity) session.get(EmpleadoEntity.class, id);
		session.flush();
		session.getTransaction().commit();
		session.close();
		return ent;
	}
}
